package cc.vv.btpartyjob.jnimodule.server

import android.app.Activity
import android.content.Context
import android.os.Handler
import android.os.Message
import android.text.TextUtils
import android.widget.TextView
import cc.vv.baselibrary.activity.UtimingBaseActivity
import cc.vv.baselibrary.util.FileMD5Util
import cc.vv.baselibrary.util.LKAppUtil
import cc.vv.baselibrary.util.LKDialogUtil
import cc.vv.baselibrary.vFinal.BTKey
import cc.vv.baselibrary.view.pickerview.adapter.ArrayWheelAdapter
import cc.vv.baselibrary.view.pickerview.lib.WheelView
import cc.vv.baselibrary.view.util.DateChoseDialog
import cc.vv.btpartyjob.jnimodule.R
import cc.vv.btpartyjob.jnimodule.moudel.req.FileUpRequestObj
import cc.vv.lklibrary.log.LogOperate
import java.io.File
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.LinkedHashMap

/**
 * 单位报到新增资源Server
 * Created by Chen.qi on 2018/10/27.
 */
object ResourceChooseServer {
    /**
     * 选择时间
     */
    fun chooseData(context: Context, mHandler: Handler) {
        val utimingBaseActivity: UtimingBaseActivity
        try {
            utimingBaseActivity = context as UtimingBaseActivity
            utimingBaseActivity.closeInput()
        } catch (e: Exception) {
        }
        val birthday = ""
        val birthArr = birthday.split("-".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
        var year = Calendar.getInstance().get(Calendar.YEAR)
        var month = Calendar.getInstance().get(Calendar.MONTH)
        var day = Calendar.getInstance().get(Calendar.DAY_OF_MONTH)
        if (birthArr.size == BTKey.BTKEY_INT_3) {
            try {
                year = Integer.parseInt(birthArr[0])
                month = Integer.parseInt(birthArr[1]) - 1
                day = Integer.parseInt(birthArr[2])
            } catch (e: Exception) {
                LogOperate.e(e.message)
            }
        }
        //初始化显示控件
        DateChoseDialog.getInstance().initBirthDatePicker(context, year, month, day, DateChoseDialog.DATE_TYPE_2)
        DateChoseDialog.getInstance().setBrithDatenterface { date ->
            val sdf = SimpleDateFormat("yyyy-MM-dd")
            val format = sdf.format(date)
            val msg = Message()
            msg.what = 10
            msg.obj = format
            mHandler?.sendMessage(msg)
        }
        DateChoseDialog.getInstance().showBirthDateDialog()
    }


    /**
     * 滚轮dialog
     */
    fun getWheelerWayDialog(activity: Activity, title: String, mHandler: Handler?, what: Int, strList: List<String>, currentItem: Int = 0) {
        val utimingBaseActivity: UtimingBaseActivity
        try {
            utimingBaseActivity = activity as UtimingBaseActivity
            utimingBaseActivity.closeInput()
        } catch (e: Exception) {
        }
        val view = activity.layoutInflater.inflate(R.layout.layout_select_cancel_reason_dialog, null)
        val dialog = LKDialogUtil.getBottomDialog(activity, view)
        //取消
        val tvCancel = view.findViewById<TextView>(R.id.tv_lscrd_cancel)
        //确定
        val tvCommit = view.findViewById<TextView>(R.id.tv_lscrd_commit)
        //标题
        val tvTitle = view.findViewById<TextView>(R.id.tv_lscrd_title)
        tvTitle.text = title
        //滚动控件
        val wvWheel = view.findViewById<WheelView>(R.id.wv_lscrd_select_cancel_reason)

        val fontSize = 16f
        val lineSpacing = 2.5f
        val itemVisibleCount = 7
        var pos = 0

        wvWheel.setTextSize(fontSize)
        wvWheel.setCyclic(false)
        wvWheel.setLineSpacingMultiplier(lineSpacing)
        wvWheel.itemsVisible = itemVisibleCount
        wvWheel.adapter = ArrayWheelAdapter(strList, strList.size)
        wvWheel.currentItem = currentItem
        wvWheel.setOnItemSelectedListener { index ->
            pos = index
        }
        tvCancel.setOnClickListener { dialog.dismiss() }

        tvCommit.setOnClickListener {
            val selStr = strList[pos]
            if (!TextUtils.isEmpty(selStr)) {
                val msg = Message()
                msg.what = what
                msg.obj = selStr + ""
                mHandler?.sendMessage(msg)
            }
            dialog.dismiss()
        }
        dialog.show()
    }


    /**
     * 选择时间
     */
    fun chooseData(context: Context, mHandler: Handler, what: Int, dataType: Int = 3) {
        val utimingBaseActivity: UtimingBaseActivity
        try {
            utimingBaseActivity = context as UtimingBaseActivity
            utimingBaseActivity.closeInput()
        } catch (e: Exception) {
        }
        val birthday = ""
        val birthArr = birthday.split("-".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
        var year = Calendar.getInstance().get(Calendar.YEAR)
        var month = Calendar.getInstance().get(Calendar.MONTH)
        var day = Calendar.getInstance().get(Calendar.DAY_OF_MONTH)
        if (birthArr.size == BTKey.BTKEY_INT_3) {
            try {
                year = Integer.parseInt(birthArr[0])
                month = Integer.parseInt(birthArr[1]) - 1
                day = Integer.parseInt(birthArr[2])
            } catch (e: Exception) {
                LogOperate.e(e.message)
            }
        }
        //初始化显示控件
        DateChoseDialog.getInstance().initBirthDatePicker(context, year, month, day, dataType)
        DateChoseDialog.getInstance().setBrithDatenterface { date ->
            val sdf = SimpleDateFormat("yyyy-MM-dd")
            val format = sdf.format(date)
            val msg = Message()
            msg.what = what
            msg.obj = format
            mHandler?.sendMessage(msg)
        }
        DateChoseDialog.getInstance().showBirthDateDialog()
    }

    /**
     * 文件上传
     */
    fun uploadFile(filePath: String): FileUpRequestObj? {
        val requestObj = FileUpRequestObj()
        val file = File(filePath)
        if (file.exists()) {
//            val imgSuffix = filePath.substring(filePath.lastIndexOf(".") + 1)
            requestObj.file = file
            requestObj.md5 = FileMD5Util.getFileMD5(file)
            return requestObj
        } else {
            return null
        }
    }


}